package cn.szzsi._admin;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.Product;
import cn.szzsi.common.model.ProductCategory;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Created by yuanzheng on 2016/9/22.
 * 商户商品分类
 */
public class ProductCategoryController extends BaseAdminController<ProductCategory> {
    private ProductCategory productCategory;
    private String shopId;

    // 商户商品分类列表
    public void list() {
        shopId=getAttr("shopId");
        setAttr("productCategoryTreeList", ProductCategory.dao.getByUid(shopId));
        render("product_category_list.html");
    }


    // 添加
    public void add() {
        shopId=getAttr("shopId");
        setAttr("productCategoryTreeList", ProductCategory.dao.getByUid(shopId));
        setAttr("shopId",shopId);
        render("product_category_input.html");
    }

    // 编辑
    public void edit() {
        shopId=getAttr("shopId");
        String id = getPara("id","");
        if(StrKit.notBlank(id)){
            setAttr("productCategory", ProductCategory.dao.findById(id));
        }
        setAttr("productCategoryTreeList", ProductCategory.dao.getByUid(shopId));
        setAttr("shopId",shopId);
        render("product_category_input.html");
    }
    // 保存
    //@Before(BusinessProductCategoryValidator.class)
    public void save(){
        shopId=getAttr("shopId");
        productCategory = getModel(ProductCategory.class);
        String parentId = getPara("parentId",null);
        productCategory.set("parent_id", parentId);
        productCategory.set("uid",shopId);
        saved(productCategory);
        if (StringUtils.isNotEmpty(parentId)) {
            ProductCategory parent = ProductCategory.dao.findById(parentId);
            if (parent != null) {
                String parentPath = parent.getStr("path");
                productCategory.set("path",parentPath + ProductCategory.PATH_SEPARATOR + productCategory.getStr("id"));
            }
        } else {
            productCategory.set("path",productCategory.getStr("id"));
        }
        updated(productCategory);
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
        //redirect("/shop/productCategroy/list");
    }

    // 更新
    public void update(){
        productCategory = getModel(ProductCategory.class);
        System.out.println(JsonKit.toJson(productCategory));;
        updated(productCategory);
        renderJson(Ret.ok("info","修改成功!").set("url","list"));
        //redirect("/shop/productCategroy/list");
    }

    // 删除
    public void delete(){
        String id = getPara("id","");
        if (StrKit.notBlank(id)){
            ProductCategory productCategory = ProductCategory.dao.findById(id);
            // 是否存在下级
            List<ProductCategory> childrenProductCategoryList = productCategory.getChildren();
            if (childrenProductCategoryList != null && childrenProductCategoryList.size() > 0) {
                ajaxJsonErrorMessage("此商品分类存在下级分类，删除失败!");
                return;
            }
            // 是否存在商品
            List<Product> productList = productCategory.getProductList();
            if (productList != null && productList.size() > 0) {
                ajaxJsonErrorMessage("此商品分类下存在商品，删除失败!");
                return;
            }
            productCategory.delete();
            ajaxJsonSuccessMessage("删除成功！");
        }
    }
}
